#常用工具

#初始化配置
function init_config()
{
  #本地数据库配置
  local_mysql_db=test
  local_mysql_charset=utf8
  local_sql_log=on

  #远程数据库配置
  remote_mysql_db=test
  remote_mysql_charset=utf8
  remote_sql_log=on
}

#初始化时间参数
function init_date()
{
  the_day="$1"

  if [ -z "$the_day" ]; then
    the_day=`date +%Y%m%d`
  fi

  the_month=`date +%Y%m -d "$the_day"`
  the_year=`date +%Y -d "$the_day"`
  prev_day=`date +%Y%m%d -d "$the_day 1 day ago"`
  next_day=`date +%Y%m%d -d "$the_day 1 day"`
  prev_month=`date +%Y%m -d "${the_month}01 1 month ago"`
  next_month=`date +%Y%m -d "${the_month}01 1 month"`
}

#执行本地数据库sql语句
function execute_sql()
{
  local sql="set names $local_mysql_charset;"
  if [ -z "$1" ]; then
    sql="$sql"`cat`
  else
    sql="$sql$1"
  fi

  if [ "$local_sql_log" = "on" ]; then
    log "$sql"
  fi

  mysql -u root $local_mysql_db -s -N --local-infile -e "$sql"
}

#执行远程数据库sql语句
function execute_remote_sql()
{
  local sql="set names $remote_mysql_charset;"
  if [ -z "$1" ]; then
    sql="$sql"`cat`
  else
    sql="$sql$1"
  fi

  local host="$2"
  local extras="$3"

  if [ "$remote_sql_log" = "on" ]; then
    log "$host $extras $sql"
  fi

  mysql -h $host -u etl $extras $remote_mysql_db -s -N -e "$sql"
}

#生成分表号
#range_num 0 99
#range_num 99 0
#range_num 0 98 2
function range_num()
{
  echo $1 $2 $3 | awk '{
    num_begin=$1
    num_end=$2
    span=$3>0?$3:1

    size=length(num_end)
    if(length(num_begin)>length(num_end)) size=length(num_begin)

    if(num_begin<num_end){
      while(num_begin<num_end){
        printf("%0*d\n",size,num_begin);
        num_begin+=span
      }
    }else{
      while(num_begin>num_end){
        printf("%0*d\n",size,num_begin);
        num_begin-=span
      }
    }
    printf("%0*d\n",size,num_end);
  }'
}

#生成日期
#range_date 20120101 20120131
#range_date 201201 201212
function range_date()
{
  local date_begin="$1"
  local date_end="$2"

  local flag
  if [ "$date_begin" -gt "$date_end" ]; then
    flag="ago"
  fi

  if [ ${#date_begin} -eq 6 ]; then
    while [ "$date_begin" -ne "$date_end" ]
    do
      echo "$date_begin"
      date_begin=`date +%Y%m -d "${date_begin}01 1 month ${flag}"`
    done
  else
    while [ "$date_begin" -ne "$date_end" ]
    do
      echo "$date_begin"
      date_begin=`date +%Y%m%d -d "${date_begin} 1 day ${flag}"`
    done
  fi
  echo "$date_end"
}

#某年第几周 周开始日期 周结束日期
#range_week 20130225 20131020
function range_week()
{
  local date_begin="$1"
  local date_end="$2"

  local the_week
  local week_num
  local the_num
  local week_end
  while :; do
    if [ "$date_begin" -gt "$date_end" ]; then
      break
    fi

    the_week=`date +%Y%W -d "$date_begin"`

    week_num=`date +%w -d "$date_begin"`
    the_num=$((7-$week_num))
    week_end=`date +%Y%m%d -d "$date_begin $the_num day"`

    echo $the_week $date_begin $week_end

    date_begin=`date +%Y%m%d -d "$week_end 1 day"`
  done
}

#去掉左右空格
function trim()
{
  sed 's/^[[:space:]]*\|[[:space:]]*$//g'
}

#是否为日期格式（yyyy-mm-dd）
function is_date()
{
  echo $1 | grep '^[0-9]\{4\}\(-[0-9]\{2\}\)\{2\}$'
}

#记录日志
function log()
{
  echo `date +'%Y-%m-%d %X'`" $0 $@"
}

#在方法执行前后记录日志
function log_fn()
{
  echo `date +'%Y-%m-%d %H:%M:%S'`" $1 start"
  $1
  echo `date +'%Y-%m-%d %H:%M:%S'`" $1 end"
}

init_config
init_date
